package com.codingbetter.streampublisher;

import com.codingbetter.common.Book;
import com.codingbetter.streampublisher.config.BookBinding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


@SpringBootApplication
@EnableBinding(BookBinding.class)
@RestController
public class StreamPublisherApplication {
    @Autowired
    BookBinding bookBinding;

    @PostMapping("/publish")
    public Book publish(@RequestBody Book book) {
        bookBinding.bookOut().send(MessageBuilder.withPayload(book).build());
        return book;
    }

    public static void main(String[] args) {
        SpringApplication.run(StreamPublisherApplication.class, args);
    }

}
